package com.allqj.sfx.infrastructure;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author sjw
 * @className WebAppConfigurer
 * @date 2022/7/19 16:26
 * @description: 注册拦截器
 */
@Component
public class WebAppConfigurer implements WebMvcConfigurer {

    private final UserPermissionInterceptor userPermissionInterceptor;

    public WebAppConfigurer(UserPermissionInterceptor userPermissionInterceptor) {
        this.userPermissionInterceptor = userPermissionInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userPermissionInterceptor)
                .addPathPatterns(
                        "/v1/**"
                )
                .excludePathPatterns(
                        "/v1/member/admin/user/login"
                   //     "/swagger-ui/*"
                );
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射路径
        registry.addMapping("/**")
                //放行哪些原始域
                .allowedOriginPatterns("*")
                //是否发送Cookie信息
                .allowCredentials(true)
                //放行哪些原始域(请求方式)
                .allowedMethods("GET", "OPTIONS", "POST", "PUT", "DELETE")
                //放行哪些原始域(头部信息)
                .allowedHeaders("*")
                //暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
                .exposedHeaders("");
    }
}
